package com.atkxp.dao.impl;

import com.atkxp.dao.BookDao;
import com.atkxp.domain.Book;
import com.atkxp.utils.JdbcUtils;

import java.util.List;

/**
 * @author user
 * @declaration
 * @create 2022/9/25-10:22
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql = "INSERT INTO t_book(`name` , `author` , `price` , `sales` , `stock` , `img_path`) values(?, ?, ?, ?, ?, ?)";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImg_path());
    }

    @Override
    public int deleteBookById(int id) {
        String sql = "delete from t_book where id = ?";
        return update(sql, id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set `name` = ? , `author`= ? , `price`=? , `sales`=? , `stock`=? , `img_path`=? where id = ?";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImg_path(), book.getId());
    }

    @Override
    public Book queryBookById(int id) {
        String sql = "select * from t_book where id = ?";
        return getInstance(sql, id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select * from t_book";
        return getAllInstance(sql);
    }

    @Override
    public Long queryBookForPageTotalCount() {
        String sql = "select count(*) from t_book";
        Long i = GetValue(sql);
        return i;
    }

    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) {
        String sql = "select * from t_book limit ?, ?";
        return getAllInstance(sql, begin, pageSize);
    }

    @Override
    public Long queryBookForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        Long i = GetValue(sql, min, max);
        return i;
    }

    @Override
    public List<Book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select * from t_book where price between ? and ? order by price limit ?, ?";
        return getAllInstance(sql, min, max, begin, pageSize);
    }
}
